package StrategyServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.StrategyDao;
import model.Strategy;

/**
 * Servlet implementation class UeditorServlet
 */
@WebServlet("/UeditorServlet")
public class UeditorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UeditorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		 //设置请求和响应的编码统一为UTF-8
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out =  response.getWriter();
        //拿到编辑器的内容
        String content = request.getParameter("editorValue");
        String name = request.getParameter("name");
        Date time= new java.sql.Date(new java.util.Date().getTime());
 
        //如果不为空
        if(content != null && name != null){
            //将内容设置进属性
            Strategy strategy = new Strategy();
            strategy.setTime(time);
            strategy.setName(name);
            strategy.setInfo(content);
            try {
    			StrategyDao.addStrategy(strategy);
    			out.print("<script>alert('添加成功');window.location.href='admin/AdminStrategyList.jsp'</script>");
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			out.print("<script>alert('添加失败');window.location.href='admin/AdminStrategyList.jsp'</script>");
    		}
        }else {
        	out.print("<script>alert('请填写内容和标题');window.location.href='admin/AdminStrategyList.jsp'</script>");
        }

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
